<template>
  <!-- <a-upload
    v-model:file-list="fileList"
    name="fileData"
    :action="action"
    :data="dataCallback"
    :headers="headers"
    :showUploadList="false"
    multiple
    @change="onChange"
    :beforeUpload="onBeforeUpload"
    :customRequest="customRequest"
  >
    <div style="width: 98px" @click="onClick">本地上传</div>
  </a-upload> -->
  <input type="file" id="file_headerpic" @blur="onBlur" />
  <label
    for="file_headerpic"
    style="width: 100%; display: block"
    @click="onClick"
  >
    本地上传
  </label>
</template>
<script setup>
import { ref } from 'vue'
import {
  usePublishCount,
  useUploadLocalFileList,
  useGroupId,
  useDataSourceManager,
} from './provide-context.js'
import { message as Message } from 'ant-design-vue'
import { useAccountStore } from '@/stores/account'
import { storeToRefs } from 'pinia'
import { uploadFile } from '@/utils/util.js'

/**
 * 事件定义
 */
let emit = defineEmits(['end', 'start'])
/**
 * 全局
 */
let publishCount = usePublishCount()
let fileList = useUploadLocalFileList()
let groupId = useGroupId()
let { update } = useDataSourceManager()
/**
 * 用户信息
 */
let accountStore = useAccountStore()
let account = storeToRefs(accountStore)
/**
 * 交互
 */
let loading = ref(false)
// function onBeforeUpload() {
//   console.log(1)
//   emit('start')
//   loading.value = true
//   publishCount.value++
//   return false
// }
// let onChange = info => {
//   console.log(2)
//   if (info.file.status === 'done') {
//     emit('end')
//     loading.value = false
//     publishCount.value--
//     if (!info?.file?.response?.success) {
//       Message.error(
//         `上传失败<${info?.file?.response?.errorMessage || 'error'}>`,
//       )
//       return
//     }
//     try {
//       //刷新列表
//       update()
//     } catch (e) {}
//   } else if (info.file.status === 'error') {
//     emit('end')
//     loading.value = false
//     publishCount.value--
//     Message.error(`上传失败<${info?.file?.error?.message || 'error'}>`)
//   }
// }
let headers = {
  Authorization: 'Bearer ' + account.token.value,
}
function dataCallback() {
  return {
    parentId: groupId.value,
    gmtBatchUpload: batchTime.value,
  }
}
/**
 * 设置上传批次时间
 */
let batchTime = ref()
function onClick() {
  batchTime.value = Date.now()
}
/**
 * 地址
 */
let action =
  (!APP_MOCK_ENABLE ? import.meta.env.VITE_APP_API_BASE || '' : '') +
  '/data/ed/file/upload'

async function onBlur(event) {
  let file = event.target.files[0]
  // console.log(file)
  let { cancel, upload } = uploadFile(
    [
      action,
      dataCallback(),
      {
        headers,
        onUploadProgress: progressEvent => {
          let percent = Math.round(
            (progressEvent.loaded * 100) / progressEvent.total,
          )
          console.log(`上传进度: ${percent}%`)
        },
      },
    ],
    file,
  )
  // try{
  //   await upload

  // }catch(e){

  // }
  setTimeout(() => {
    cancel()
  }, 1000)
  console.log(cancel)
}
</script>
<style lang="less" scoped>
input[type='file'] {
  display: none;
  opacity: 0;
}
</style>
